package com.hjq.fcloud.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Handler
import android.os.Message
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.hjq.fcloud.R
import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.utils.FileUtils
import kotlinx.android.synthetic.main.activity_pdf.webView

/**
 *   公共pdf查看
 */
class CommonPdfViewActivity : AppActivity() {

    companion object{
        const val INTENT_IN_PATH = "path"
        /**
         * 通过腾讯tbs打开文件
         */
        fun start(activity: Context, path: String) {
            var intent = Intent(activity,CommonPdfViewActivity::class.java)
            intent.putExtra(INTENT_IN_PATH,path)
            activity.startActivity(intent)
        }
    }

    private val url by lazy { getString(INTENT_IN_PATH) }


    private val mHandler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                2222 -> {
                    hideDialog()
                }
            }
        }
    }


    override fun getLayoutId(): Int {
        return R.layout.activity_pdf
    }

    override fun initView() {
        setTitle(FileUtils.getFileName(url))
    }

    override fun initData() {
        showDialog()

        initWebView()
    }

    private fun initWebView() {
        configWebView(webView.settings)
        webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(webView: WebView, s: String) {
                super.onPageFinished(webView, s)
                mHandler.sendEmptyMessage(2222)
            }
        }
        webView.loadUrl("file:///android_asset/pdf.html?" + url)
    }

    private fun configWebView(webSetting: WebSettings) {
//        webSetting.setAllowFileAccess(true) //访问本地文件
//        webSetting.setLayoutAlgorithm(webSetting.LayoutAlgorithm.NARROW_COLUMNS)
//        webSetting.setSupportZoom(true)
//        webSetting.setSavePassword(false)
//        webSetting.setBuiltInZoomControls(true) //设置内置的缩放控件。若为false，则该WebView不可缩放
//        webSetting.setDisplayZoomControls(true) //隐藏原生的缩放控件，默认是true。如果不想显示放大和缩小按钮，将该属性设置为false即可
//        webSetting.setUseWideViewPort(true)
//        webSetting.setSupportMultipleWindows(false)
//        webSetting.setAppCacheEnabled(true)
//        webSetting.setDomStorageEnabled(true)
//        webSetting.setCacheMode(webSetting.LOAD_NO_CACHE)
//        webSetting.setJavaScriptEnabled(true)
//        webSetting.setGeolocationEnabled(true)
//        webSetting.setAppCacheMaxSize(Long.MAX_VALUE)
//        webSetting.setAppCachePath(context.getDir("appcache", 0).getPath())
//        webSetting.setDatabasePath(context.getDir("databases", 0).getPath())
//        webSetting.setGeolocationDatabasePath(context.getDir("geolocation", 0).getPath())
//        webView.settings.cacheMode = webSetting.LOAD_CACHE_ELSE_NETWORK
//        webView.settings.setRenderPriority(webSetting.RenderPriority.HIGH)
//        webView.settings.allowUniversalAccessFromFileURLs = true
//        // 支持多窗口
//        webView.settings.setSupportMultipleWindows(true)
//        // 开启 DOM storage API 功能
//        webView.settings.domStorageEnabled = true
//        // 开启 Application Caches 功能
//        webView.settings.setAppCacheEnabled(true)

        webSetting.cacheMode = WebSettings.LOAD_DEFAULT
        //设置加载进来的页面自适应手机屏幕
        webSetting.useWideViewPort = true
        webSetting.loadWithOverviewMode = true
        //问题2：基本都需要支持JS
        webSetting.javaScriptEnabled = true
        webSetting.allowFileAccess = true
        //支持通过JS打开新窗口
        webSetting.allowFileAccessFromFileURLs = true
        webSetting.allowUniversalAccessFromFileURLs = true
        webSetting.javaScriptCanOpenWindowsAutomatically = true
        webSetting.setGeolocationEnabled(true)
        webSetting.domStorageEnabled = true
        webSetting.setAppCacheEnabled(true)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //http与https的方法
            webSetting.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
    }

}